const ZERO_6E = 0.000001;
class LgxVector4D {
    constructor() {
        this.x = 0;
        this.y = 0;
        this.z = 0;
        this.w = 1;
    }
    colone(src) {
        this.x = src.x;
        this.y = src.y;
        this.z = src.z;
        this.w = src.w;
    }
    setBy4Val(px, py, pz, pw) {
        this.x = px;
        this.y = py;
        this.z = pz;
        this.w = pw;
    }
    setByArray(pValue) {
        this.x = pValue[0];
        this.y = pValue[1];
        this.z = pValue[2];
        this.w = pValue[3];
    }

    setBy3DPt(v3) {
        this.x = v3.x;
        this.y = v3.y;
        this.z = v3.z;
        this.w = 1;
    }


    addVal(pValue) {
        let x1 = this.x + pValue;
        let y1 = this.y + pValue;
        let z1 = this.z + pValue;
        let w1 = this.w + pValue;
        let rst = new LgxVector4D();
        rst.setBy4Val(x1, y1, z1, w1);
        return rst;
    }
    subVal(pValue) {
        let x1 = this.x - pValue;
        let y1 = this.y - pValue;
        let z1 = this.z - pValue;
        let w1 = this.w - pValue;
        let rst = new LgxVector4D();
        rst.setBy4Val(x1, y1, z1, w1);
        return rst;
    }

    mulVal(pValue) {
        let x1 = this.x * pValue;
        let y1 = this.y * pValue;
        let z1 = this.z * pValue;
        let w1 = this.w * pValue;
        let rst = new LgxVector4D();
        rst.setBy4Val(x1, y1, z1, w1);
        return rst;
    }

    divVal(pValue) {
        let x1 = this.x / pValue;
        let y1 = this.y / pValue;
        let z1 = this.z / pValue;
        let w1 = this.w / pValue;
        let rst = new LgxVector4D();
        rst.setBy4Val(x1, y1, z1, w1);
        return rst;
    }


    invert() {
        let x1 = -this.x;
        let y1 = -this.y;
        let z1 = -this.z;
        let w1 = this.w;
        let rst = new LgxVector4D();
        rst.setBy4Val(x1, y1, z1, w1);
        return rst;
    }

    addOther(pVector) {
        let x1 = this.x + pVector.x;
        let y1 = this.y + pVector.y;
        let z1 = this.z + pVector.z;
        let rst = new LgxVector4D();
        rst.setBy4Val(x1, y1, z1, 1);
        return rst;
    }


    subOther(pVector) {
        let x1 = this.x - pVector.x;
        let y1 = this.y - pVector.y;
        let z1 = this.z - pVector.z;
        let rst = new LgxVector4D();
        rst.setBy4Val(x1, y1, z1, 1);
        return rst;
    }

    mulOther(pVector) {
        let x1 = this.x * pVector.x;
        let y1 = this.y * pVector.y;
        let z1 = this.z * pVector.z;
        let rst = new LgxVector4D();
        rst.setBy4Val(x1, y1, z1, 1);
        return rst;
    }
    divOther(pVector) {
        let x1 = this.x / pVector.x;
        let y1 = this.y / pVector.y;
        let z1 = this.z / pVector.z;
        let rst = new LgxVector4D();
        rst.setBy4Val(x1, y1, z1, 1);
        return rst;
    }




   //向量点乘
    DotProduct(pVector) {
        let val = this.x * pVector.x + this.y * pVector.y + this.z * pVector.z;
        return val;
    }

    //向量叉乘
    CrossProduct(pVector) {
        let x1 = this.y * pVector.z - pVector.y * this.z;
        let y1 = pVector.x * this.z - this.x * pVector.z;
        let z1 = this.x * pVector.y - pVector.x * this.y;
        return LgxVector4D(x1, y1, z1);
    }


    compare(pVector) {
        let dx = Math.absf(this.x - pVector.x);
        let dy = Math.absf(this.y - pVector.y);
        let dz = Math.absf(this.z - pVector.z);
        if (dx < ZERO_6E && dy < ZERO_6E && dz < ZERO_6E) {
            return true;
        }
        else {
            return false;
        }
    }


    Length() {
        return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
    }

    SquareLength() {
        return (this.x * this.x + this.y * this.y + this.z * this.z);
    }


    Distance(p2) {
        let lRst = Math.pow((this.x - p2.x), 2) + powf((this.y - p2.y), 2) + powf((this.z - p2.z), 2);
        lRst = Math.sqrt(lRst);
        return lRst;
    }

    Normalize() {
        let lenth = this.Length();
        this.x = this.x / lenth;
        this.y = this.y / lenth;
        this.z = this.z / lenth;
    }

    //@}


    IsZero() {
        let dx = Math.abs(this.x);
        let dy = Math.abs(this.y);
        let dz = Math.abs(this.z);
        if (dx < ZERO_6E && dy < ZERO_6E && dz < ZERO_6E) {
            return true;
        }
        else {
            return false;
        }
    }


}

export { LgxVector4D }